package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author leo
 * @since 2021-11-24
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public void getPageByCondition(IPage<Teacher> page, TeacherQuery teacherQuery) {
        String name = teacherQuery.getName() ;
        Integer level = teacherQuery.getLevel() ;
        String joinDateBegin = teacherQuery.getJoinDateBegin() ;
        String joinDateEnd = teacherQuery.getJoinDateEnd() ;
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>() ;
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like(Teacher::getName,name) ;
        }
        if (!StringUtils.isEmpty(joinDateBegin)) {
            queryWrapper.ge(Teacher::getJoinDate,joinDateBegin) ;
        }
        if (!StringUtils.isEmpty(joinDateEnd)) {
            queryWrapper.le(Teacher::getJoinDate,joinDateEnd) ;
        }
        if (level != null) {
            queryWrapper.eq(Teacher::getLevel,level) ;
        }
        baseMapper.selectPage(page,queryWrapper) ;

    }

    @Override
    @Cacheable(value = "teacher",key = "'hotTeachers'")
    public List<Teacher> getHotTeachers() {
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.orderByDesc(Teacher::getSort) ;
        queryWrapper.last("limit 4") ;
        return this.list(queryWrapper);
    }
}
